//
//  TKIMBaseMsgView.m
//  TKIM
//
//  Created by liupm on 15/10/14.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMBaseMsgView.h"
//#import "TKIMImageHelper.h"
//#import "TKIMImageHelper.h"


@interface TKIMBaseMsgView ()
@end

@implementation TKIMBaseMsgView

@synthesize message = _message;

-(instancetype)initWithFrame:(CGRect)frame  msg:(TKIMMessage *)msg
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = YES;
        _userNameHeight = 5;
        [self drawView];
        self.message = msg;
    }
    return self;
}

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [self addSubview:self.headImageView];
    [self addSubview:self.backgroundImageView];
    [self addSubview:self.userNameLabel];
    [self.backgroundImageView addSubview:self.msgSateView];
}

#pragma mark 属性
/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  头像
 */
-(UIImageView *)headImageView
{
    if (!_headImageView)
    {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TKIMChatMsgHorizontalPadding, 0, TKIMChatHeadImageViewWidth, TKIMChatHeadImageViewWidth)];
        _headImageView.backgroundColor = [UIColor clearColor];
        _headImageView.aliCornerRadius = _headImageView.width/2;
//        _headImageView.image = TKIMGetImage(@"man-head");
        //点击头像事件
        _headImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headImageViewTap:)];
        [_headImageView addGestureRecognizer:tap];
    }
    return _headImageView;
}




-(UILabel *)userNameLabel
{
    if (!_userNameLabel)
    {
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.backgroundImageView.left, 0,TKIMScreenWidth*0.5, _userNameHeight)];
        _userNameLabel.backgroundColor = [UIColor clearColor];
        _userNameLabel.font = [UIFont systemFontOfSize:13.0f];
        _userNameLabel.textColor = [UIColor grayColor];
        _userNameLabel.hidden=YES;
        
    }
    return _userNameLabel;
}


-(UIImageView *)backgroundImageView
{
    if (!_backgroundImageView)
    {
        _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.headImageView.right+TKIMChatMsgHorizontalPadding, _userNameHeight, self.width, self.height)];
//       _backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.headImageView.right+TKIMChatMsgHorizontalPadding, 0, self.width, self.height)];
        _backgroundImageView.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [_backgroundImageView addGestureRecognizer:longPress];
    }
    return _backgroundImageView;
}


-(UIImageView *)msgSateView
{
    if (!_msgSateView)
    {
        _msgSateView = [[UIImageView alloc] initWithFrame:CGRectMake(self.backgroundImageView.right+3, (self.backgroundImageView.height-30)/2, 30, 30)];
        _msgSateView.contentMode = UIViewContentModeCenter;
        _msgSateView.animationDuration = 0.8f;
    }
    return _msgSateView;
}



-(void)startAnimating
{
    if(!self.msgSateView.isAnimating)
    {
        self.msgSateView.animationImages = @[TKIMGetImage(@"im_icon_loading0"),
                                             TKIMGetImage(@"im_icon_loading1"),
                                             TKIMGetImage(@"im_icon_loading2"),
                                             TKIMGetImage(@"im_icon_loading3")];
        [self.msgSateView startAnimating];
    }
}


-(void)stopAnimating
{
    if(self.msgSateView.isAnimating)
    {
        [self.msgSateView stopAnimating];
    }
}


-(void)showError:(BOOL)show
{
    if (show)
    {
        _msgSateView.image = TKIMGetImage(@"im_icon_fail");
    }
    else
    {
        _msgSateView.image = nil;

    }
}


-(void)setMessage:(TKIMMessage *)message
{
    _message = message;
}


/**
 *  @Author lpm, 15-07-27 17:07:43
 *
 *  @brief  设置背景
 *
 *  @param backgroundImage
 */
-(void)setBackgroundImage:(UIImage *)backgroundImage
{
    _backgroundImage = backgroundImage;
    _backgroundImage = [_backgroundImage stretchableImageWithLeftCapWidth:_backgroundImage.size.width * 0.5 topCapHeight:_backgroundImage.size.height*0.7];
    self.backgroundImageView.image = _backgroundImage;
}


/**
 *  @Author lpm, 15-11-06 14:11:34
 *
 *  @brief  获得消息的frame
 *
 *  @param message
 *  @param size
 *
 *  @return
 */
-(CGRect)getMsgFrame:(TKIMMessage *)message size:(CGSize)size
{

    if(message.chatUserType == TKIMUserTypeSys){
        self.headImageView.image = TKIMGetImage(@"IM_black_ic_sysMessage_0");
    }else{
        __weak typeof(self) weakSelf=self;
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            __strong typeof(self)strongSelf =weakSelf;
            if (message.isSender) {
//                dispatch_async(dispatch_get_main_queue(), ^{
                    strongSelf.headImageView.image = TKIMGetImage(@"infohead");
//                });
            }else{
//                dispatch_async(dispatch_get_main_queue(), ^{
                    strongSelf.headImageView.image = TKIMGetImage(@"im_icon_userDefault");
//                });
            }
//        });
    }

    self.userNameLabel.text = message.senderName;
    if (message.type == TKIMMsgTypeTxt || message.type == TKIMMsgTypeScore)
    {
        //设置消息背景图片大小
        self.backgroundImageView.frame = CGRectMake(0,_userNameHeight, size.width+40, size.height + 20);
    }
    else
    {
        //设置消息背景图片大小
        self.backgroundImageView.frame = CGRectMake(0,_userNameHeight, size.width+12, size.height+6);
    }
    
    //消息容器大小
    CGRect rect = [self getFrameSize:self.backgroundImageView.frame.size];
    
    //设置消息的位置
    [self setMsgFrame:rect message:message];
    
    //设置消息label的大小
    CGRect msgRect = CGRectIntegral(CGRectMake(0,0, size.width, size.height));
    
    msgRect.origin.x = (self.backgroundImageView.width - size.width)/2;
    msgRect.origin.y = (self.backgroundImageView.height - size.height)/2;
    
    if(message.isSender)
    {
        msgRect.origin.x -= 3;
    }
    else
    {
        msgRect.origin.x += 3;
    }
    return msgRect;
}



/**
 *  @Author lpm, 15-10-22 17:10:14
 *
 *  @brief  设置消息的位置
 *
 *  @param rect
 *  @param message
 */
-(void)setMsgFrame:(CGRect)rect message:(TKIMMessage *)message
{
    CGFloat frameWidth = rect.size.width;
    CGFloat frameHeight = rect.size.height;
    CGFloat frameX = 0;
    CGFloat frameY = 0;
    

    
    //如果是本人发送的消息
    if (message.isSender)
    {
        self.userNameLabel.textAlignment = NSTextAlignmentRight;
        self.backgroundImage =  TKIMGetImage(TKIMChatMsgSenderImage);
        frameX = TKIMScreenWidth-rect.size.width;
        frameY = self.frame.origin.y;
        self.frame = CGRectMake(frameX, frameY, frameWidth, frameHeight);
        
        //设置消息背景图片大小
        self.backgroundImageView.frame = CGRectMake(
                                                    0,
                                                    self.backgroundImageView.top,
                                                    self.backgroundImageView.width,
                                                    self.backgroundImageView.height
                                                    );
        
        self.headImageView.frame = CGRectMake(
                                              self.backgroundImageView.width+self.backgroundImageView.left+TKIMChatMsgHorizontalPadding,
                                              self.headImageView.top,
                                              TKIMChatHeadImageViewWidth,
                                              TKIMChatHeadImageViewWidth);
        
        self.msgSateView.frame = CGRectMake(-33, (self.backgroundImageView.height-30)/2, 30, 30);
        
        if (message.state == TKIMMsgSendStateSending)
        {
            [self startAnimating];
        }
        else
        {
            [self stopAnimating];
            if (message.state == TKIMMsgSendStateSendFail)
            {
                [self showError:YES];
            }
            else
            {
                [self showError:NO];
            }
        }
    }
    else
    {
        [self showError:NO];

        self.userNameLabel.textAlignment = NSTextAlignmentLeft;
        self.backgroundImage = TKIMGetImage(TKIMChatMsgReceiverImage);
        frameX = 0.0;
        frameY = self.frame.origin.y;
        self.frame = CGRectMake(frameX , frameY,frameWidth , frameHeight);
        self.headImageView.frame = CGRectMake(
                                              TKIMChatMsgHorizontalPadding,
                                              self.headImageView.top,
                                              TKIMChatHeadImageViewWidth,
                                              TKIMChatHeadImageViewWidth
                                              );
        
        //设置消息背景图片大小
        self.backgroundImageView.frame = CGRectMake(
                                                    self.headImageView.right+TKIMChatMsgHorizontalPadding,
                                                    self.backgroundImageView.top,
                                                    self.backgroundImageView.width,
                                                    self.backgroundImageView.height
                                                    );
        
        self.msgSateView.frame = CGRectMake(
                                            self.backgroundImageView.left+self.backgroundImageView.width+3,
                                            (self.backgroundImageView.height-30)/2, 30, 30
                                            );
    }
    
    
    if (!message.isSender)
    {
        self.userNameLabel.frame =CGRectMake(
                                             self.backgroundImageView.left,
                                             0,
                                             TKIMScreenWidth*0.5,
                                             _userNameHeight);
    }
    else
    {
        self.userNameLabel.frame =CGRectMake(
                                             self.backgroundImageView.left+self.backgroundImageView.width-TKIMScreenWidth*0.5,
                                             0,
                                             TKIMScreenWidth*0.5,
                                             _userNameHeight);
    }
    
    if (message.channel==1) {
        
        self.userNameLabel.hidden=YES;
        
    }
    
    message.size = rect.size;
}



/**
 *  @Author lpm, 15-11-06 12:11:43
 *
 *  @brief  获得容器的大小
 *
 *  @param size
 *
 *  @return
 */
-(CGRect)getFrameSize:(CGSize)size
{
    //消息容器大小
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    rect = CGRectIntegral(rect);
    rect.size.width = size.width+TKIMChatHeadImageViewWidth+TKIMChatMsgHorizontalPadding*2;
    rect.size.height= size.height + _userNameHeight + TKIMChatMsgPadding;
    return rect;
}




-(void)longPress:(UILongPressGestureRecognizer *)press
{
    if (press.state == UIGestureRecognizerStateBegan)
    {
        [self routerEventWithName:TKIMMsgLongPressEvent userInfo:@{@"msg":_message,@"view":self}];
    }
}


/**
 *  @author zhengrs, 16-01-26 11:01:12
 *
 *  @brief 点击头像事件
 *
 *  @param tap
 */
-(void)headImageViewTap:(UITapGestureRecognizer *)tap
{
    [self routerEventWithName:TKIMMsgHeadImageTapEvent userInfo:self.message];
}


-(BOOL)canBecomeFirstResponder
{
    return YES;
}

@end
